package thread;

/**
 * Created by Administrator on 2017/10/18 0018.
 */
public class NotifyDemo
{
    public static void main(String[] args) throws Exception
    {
        Object a = "A";
        Object b = "B";
        Object c = "C";
        new PrintThread(b, a).start();
        Thread.sleep(10);
        new PrintThread(a, b).start();
//        Thread.sleep(10);
//        new PrintThread(b, c).start();
    }
    
    static class PrintThread extends Thread
    {
        private Object prev;
        private Object self;
        public PrintThread(Object pv, Object self)
        {
            prev = pv;
            this.self = self;
        }
        
        @Override
        public void run()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    synchronized (prev)
                    {
                        synchronized (self)
                        {
                            System.out.print(self.toString());
                            self.notify();
                        }
                        prev.wait();
                    }
                }catch (Exception e){}
            }
        }
    }
}
